<?
require_once("../include/class/ExcelWriter.class.php");

class ExcelFactory extends ExcelWriter {

var $attribute=array();
var $filename;

	function ExcelFactory($filename,$sql,$attribute){
		$this->filename=$filename;
		$this->sql=$sql;
		if(is_array($attribute)){
			$this->attribute=array_merge($this->attribute,$attribute);
		}
		
	}
	
	function doWrite(){
		
		$this->ExcelWriter($this->filename);
		$query=mysql_query($this->sql);
		$numOfField=mysql_num_fields($query);
		$this->array_key=array_keys($this->attribute);
		$this->array_value=array_values($this->attribute);
		$num=count($this->array_key);

	
		$this->writeRow();
		$this->writeCol("No");
		for($i=0;$i<$num;$i++){
			for($k=0;$k<$numOfField;$k++){
				$name=mysql_field_name($query,$k);
				if(trim($this->array_key[$i])==trim($name)){
					$this->content=ucfirst($this->array_value[$i]);
					$this->writeCol($this->content);	
				}
			}
		
		}
		
		$j=0;
		while($data=mysql_fetch_array($query)){
		$j++;
		$this->writeRow();
		$this->writeCol($j);
		for($i=0;$i<$num;$i++){
			for($k=0;$k<$numOfField;$k++){
				$name=mysql_field_name($query,$k);
				if(trim($this->array_key[$i])==trim($name)){
					$this->content=$data[$k];
					if($k==0)
						$this->content=base64_encode($data[$k].session_id());
					$this->writeCol($this->content);	
				}
			}
		
		}

		}
		
		$this->close();
	}
	
	function getFile(){
		$this->doWrite();
		return $this->filename;
	}	
}
?>